package cn.tedu.tea.service;

import cn.tedu.tea.domain.GoodsDetail;
import cn.tedu.tea.domain.param.GoodsDetailAddNewDTO;
import cn.tedu.tea.domain.param.GoodsDetailUpdateDTO;
import cn.tedu.tea.domain.vo.GoodsDetailListVO;
import cn.tedu.tea.domain.vo.GoodsDetailStandardVO;
import cn.tedu.tea.ex.ServiceException;
import cn.tedu.tea.mapper.GoodsDetailMapper;
import cn.tedu.tea.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class GoodsDetailServiceImpl implements IGoodsDetailService {

    @Autowired
    private GoodsDetailMapper goodsDetailMapper;

    @Override
    public void addNew(GoodsDetailAddNewDTO goodsDetailAddNewDTO) {
        log.debug("开始处理【添加商品详情】的业务，参数： {}", goodsDetailAddNewDTO);
        GoodsDetail goodsDetail = new GoodsDetail();
        BeanUtils.copyProperties(goodsDetailAddNewDTO, goodsDetail);
        int rows = goodsDetailMapper.insert(goodsDetail);
        if (rows != 1){
            String message = "添加商品详情失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Integer id) {
        log.debug("开始处理【根据id删除商品详情】的业务，参数： {}", id);
        GoodsDetailStandardVO goodsDetailStandard = goodsDetailMapper.getStandardById(id);
        if (goodsDetailStandard == null){
            String message = "删除商品详情失败，要删除的商品详情不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = goodsDetailMapper.deleteById(id);
        if (rows != 1){
            String message = "删除商品详情失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        log.debug("开始处理【根据id批量删除商品详情】的业务，参数： {}", ids);
        int rows = goodsDetailMapper.deleteByIds(ids);
        if (rows < 1){
            String message = "删除商品详情失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(GoodsDetailUpdateDTO goodsDetailUpdateDTO) {
        log.debug("开始处理【修改商品详情信息】的业务，参数：{}", goodsDetailUpdateDTO);
        GoodsDetailStandardVO goodsDetailStandard = goodsDetailMapper.getStandardById(goodsDetailUpdateDTO.getId());
        if (goodsDetailStandard == null){
            String message = "修改商品详情失败，要访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        GoodsDetail goodsDetail = new GoodsDetail();
        BeanUtils.copyProperties(goodsDetailUpdateDTO, goodsDetail);
        int rows = goodsDetailMapper.update(goodsDetail);
        if (rows != 1){
            String message = "修改商品详情失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public GoodsDetailStandardVO getStandardById(Integer id) {
        log.debug("开始处理【根据id查询商品详情详情】的业务，参数： {}", id);
        GoodsDetailStandardVO goodsDetailStandard = goodsDetailMapper.getStandardById(id);
        if (goodsDetailStandard == null){
            String message = "查询商品详情详情失败，要查询的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return goodsDetailStandard;
    }

    @Override
    public List<GoodsDetailListVO> list() {
        log.debug("开始处理【查询商品详情列表】的业务，无参数");
        List<GoodsDetailListVO> list = goodsDetailMapper.list();
        return list;
    }

}
